package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.loss.domain.WellDetailData;
import com.ruoyi.loss.domain.bo.WellDetailDataBo;
import com.ruoyi.loss.domain.vo.WellDetailDataVo;
import com.ruoyi.loss.mapper.WellDetailDataMapper;
import com.ruoyi.loss.service.IWellDetailDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钻井-详细信息Service业务层处理
 *
 * @author yangyong
 * @date 2023-12-26
 */
@RequiredArgsConstructor
@Service
public class WellDetailDataServiceImpl implements IWellDetailDataService {

    private final WellDetailDataMapper baseMapper;

    /**
     * 查询钻井-详细信息
     */
    @Override
    public WellDetailDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钻井-详细信息列表
     */
    @Override
    public TableDataInfo<WellDetailDataVo> queryPageList(WellDetailDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellDetailData> lqw = buildQueryWrapper(bo);
        Page<WellDetailDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钻井-详细信息列表
     */
    @Override
    public List<WellDetailDataVo> queryList(WellDetailDataBo bo) {
        LambdaQueryWrapper<WellDetailData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellDetailData> buildQueryWrapper(WellDetailDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellDetailData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellDetailData::getWellNo, bo.getWellNo());
        return lqw;
    }

    /**
     * 新增钻井-详细信息
     */
    @Override
    public Boolean insertByBo(WellDetailDataBo bo) {
        WellDetailData add = BeanUtil.toBean(bo, WellDetailData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钻井-详细信息
     */
    @Override
    public Boolean updateByBo(WellDetailDataBo bo) {
        WellDetailData update = BeanUtil.toBean(bo, WellDetailData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WellDetailData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钻井-详细信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
